#include "BrickQuestion.h"
//#include "Global.h"
#include "Resource.h"


BrickQuestion::BrickQuestion(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State) : Object(x, y)
{
	this->SetID(ID);
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\Brick_32.png"), 150, 0);
	
	isMoveUp = false;

	if (this->GetID() == 25)	//gach hoi
	{
		_sprite->SetFrame(2,4);
	} 
	else if (this->GetID() == 26)	//gach an
	{
		_sprite->SetFrame(4,4);
		//_sprite->SetFrame(2,4);
	}
	else if (this->GetID() == 28)	//gach sao
	{
		_sprite->SetFrame(0,0);
		//_sprite->SetFrame(2,4);
	}
	//else if (this->GetID() == 29)	//gach da va cham, luu tu map len
	//{
	//	_sprite->SetFrame(1,1);
	//	//_sprite->SetFrame(2,4);
	//	isMoveUp = true;
	//}

	_xOld = _x;
	_yOld = _y;

	_vx = 0;
	_vy = 0;
	_accelY = 0;
	


	//object_State = BS_NORMAL; mac dinh object state la NORMAL:1
	object_State = Object_State;
	if (object_State == BS_NONE)
	{
		_sprite->SetFrame(1,1);
		isMoveUp = true;
	} 
	 
}

BrickQuestion::~BrickQuestion()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}



void BrickQuestion::Render()
{

	switch(object_State)
	{
	case BS_NORMAL:
	case BS_NONE:
	case BS_MOVEUP:
		_sprite->Render(_x, _y);
		break;

	}


}

OBJECT_TYPE BrickQuestion::GetTypeObject()
{
	return OBJECT_LAND;
}

void BrickQuestion::ResetRect()
{
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
} 

void BrickQuestion::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	
	switch(object_State)
	{
	case BS_NORMAL:
		ResetRect();
		_sprite->Update(gameTime);
		break;

	case BS_MOVEUP:
		if (isMoveUp == false)
		{
			_vy += _accelY * gameTime;
			_y += _vy * gameTime + 1/2 * _accelY * gameTime * gameTime;
			if (_y > _yOld)
			{

				/*ChangeState(BS_NORMAL);
				SetID(29);*/
				ChangeState(BS_NONE);
			}
			ResetRect();
			_sprite->Update(gameTime);
		}
		break;

	case BS_NONE:
		ResetRect();
		_sprite->Update(gameTime);
		break;
	}

}

void BrickQuestion::ChangeState(int state)
{
	object_State = state;
	switch (object_State)
	{
	case BS_NORMAL:
		isMoveUp = true;
		_y = _yOld;
		_vy = 0;
		_accelY = 0;
		break;
	case BS_MOVEUP:
		_vy = -1.0;//-200;
		_accelY = GRAVITY;
		_sprite->SetFrame(1,1); //frame emptybox
		break;

	case BS_NONE:
		isMoveUp = true;
		_y = _yOld;
		_vy = 0;
		_accelY = 0;
		break;

	case BS_REMOVE:
		break;
	}
}